package com.yf.service.impl;

import com.yf.entity.MonthCount;
import com.yf.entity.MonthList;
import com.yf.entity.Statistics;
import com.yf.entity.Student;
import com.yf.mapper.StatisticsMapper;
import com.yf.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class StatisticsServiceImpl implements StatisticsService {

    @Autowired
    private StatisticsMapper statisticsMapper;

    @Override
    public List getStudentByLastWeek1() {
        List list = new ArrayList();
        Map<String,Object> stuMap1=new HashMap<>();
        Map<String,Object> stuMap2=new HashMap<>();
        Map<String,Object> stuMap3=new HashMap<>();
        Map<String,Object> stuMap4=new HashMap<>();
        Map<String,Object> stuMap5=new HashMap<>();
        Map<String,Object> stuMap6=new HashMap<>();
        Map<String,Object> stuMap7=new HashMap<>();

        List<Student> studentByLastWeek = statisticsMapper.getStudentByLastWeek();

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        Calendar calendar3 = Calendar.getInstance();
        Calendar calendar4 = Calendar.getInstance();
        Calendar calendar5 = Calendar.getInstance();
        Calendar calendar6 = Calendar.getInstance();
        Calendar calendar7 = Calendar.getInstance();
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;

        int offset1 = 1 - dayOfWeek;
        int offset2 = 2 - dayOfWeek;
        int offset3 = 3 - dayOfWeek;
        int offset4 = 4 - dayOfWeek;
        int offset5 = 5 - dayOfWeek;
        int offset6 = 6 - dayOfWeek;
        int offset7 = 7 - dayOfWeek;

        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        calendar3.add(Calendar.DATE, offset3 - 7);
        calendar4.add(Calendar.DATE, offset4 - 7);
        calendar5.add(Calendar.DATE, offset5 - 7);
        calendar6.add(Calendar.DATE, offset6 - 7);
        calendar7.add(Calendar.DATE, offset7 - 7);

        Set<Student> studentSet1 = new HashSet<>();
        Set<Student> studentSet2 = new HashSet<>();
        Set<Student> studentSet3 = new HashSet<>();
        Set<Student> studentSet4 = new HashSet<>();
        Set<Student> studentSet5 = new HashSet<>();
        Set<Student> studentSet6 = new HashSet<>();
        Set<Student> studentSet7 = new HashSet<>();

        for (Student student : studentByLastWeek) {

            Date inTime = student.getInTime();
            String newTime = sdf.format(inTime);

            System.out.println(newTime);

            if (newTime.equals(sdf.format(calendar1.getTime()))){
                System.out.println("周一");
                studentSet1.add(student);
            }
            else if (newTime.equals(sdf.format(calendar2.getTime()))){
                System.out.println("周2");
                studentSet2.add(student);
            }
            else if (newTime.equals(sdf.format(calendar3.getTime()))){
                System.out.println("周3");
                studentSet3.add(student);
            }
            else if (newTime.equals(sdf.format(calendar4.getTime()))){
                System.out.println("周4");
                studentSet4.add(student);
            }
            else if (newTime.equals(sdf.format(calendar5.getTime()))){
                System.out.println("周5");
                studentSet5.add(student);
            }
            else if (newTime.equals(sdf.format(calendar6.getTime()))){
                System.out.println("周6");
                studentSet6.add(student);
            }
            else if (newTime.equals(sdf.format(calendar7.getTime()))){
                System.out.println("周7");
                studentSet7.add(student);
            }

        }


        stuMap1.put("name","周一");
        stuMap1.put("y",studentSet1.size());
        stuMap1.put("drilldown","周一新增");

        stuMap2.put("name","周二");
        stuMap2.put("y",studentSet2.size());
        stuMap2.put("drilldown","周二新增");

        stuMap3.put("name","周三");
        stuMap3.put("y",studentSet3.size());
        stuMap3.put("drilldown","周三新增");

        stuMap4.put("name","周四");
        stuMap4.put("y",studentSet4.size());
        stuMap4.put("drilldown","周四新增");

        stuMap5.put("name","周五");
        stuMap5.put("y",studentSet5.size());
        stuMap5.put("drilldown","周五新增");

        stuMap6.put("name","周六");
        stuMap6.put("y",studentSet6.size());
        stuMap6.put("drilldown","周六新增");

        stuMap7.put("name","周日");
        stuMap7.put("y",studentSet7.size());
        stuMap7.put("drilldown","周日新增");

        list.add(stuMap1);
        list.add(stuMap2);
        list.add(stuMap3);
        list.add(stuMap4);
        list.add(stuMap5);
        list.add(stuMap6);
        list.add(stuMap7);

        return list;
    }

    @Override
    public List getStudentByLastWeek2() {
        //存储每天的数据
        List<Statistics> list = new ArrayList<>();

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar1 = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        Calendar calendar3 = Calendar.getInstance();
        Calendar calendar4 = Calendar.getInstance();
        Calendar calendar5 = Calendar.getInstance();
        Calendar calendar6 = Calendar.getInstance();
        Calendar calendar7 = Calendar.getInstance();
        int dayOfWeek = calendar1.get(Calendar.DAY_OF_WEEK) - 1;

        int offset1 = 1 - dayOfWeek;
        int offset2 = 2 - dayOfWeek;
        int offset3 = 3 - dayOfWeek;
        int offset4 = 4 - dayOfWeek;
        int offset5 = 5 - dayOfWeek;
        int offset6 = 6 - dayOfWeek;
        int offset7 = 7 - dayOfWeek;

        calendar1.add(Calendar.DATE, offset1 - 7);
        calendar2.add(Calendar.DATE, offset2 - 7);
        calendar3.add(Calendar.DATE, offset3 - 7);
        calendar4.add(Calendar.DATE, offset4 - 7);
        calendar5.add(Calendar.DATE, offset5 - 7);
        calendar6.add(Calendar.DATE, offset6 - 7);
        calendar7.add(Calendar.DATE, offset7 - 7);

        List<Statistics> statisticsList = statisticsMapper.getStatisticsList();
        for (Statistics statistics : statisticsList) {
            if (statistics.getData().equals(sdf.format(calendar1.getTime()))){
                statistics.setDrillDown("周一新增");
                statistics.setData("周一");
                list.add(statistics);
            }
            if (statistics.getData().equals(sdf.format(calendar2.getTime()))){
                statistics.setDrillDown("周二新增");
                statistics.setData("周二");
                list.add(statistics);
            }
            if (statistics.getData().equals(sdf.format(calendar3.getTime()))){
                statistics.setDrillDown("周三新增");
                statistics.setData("周三");
                list.add(statistics);
            }
            if (statistics.getData().equals(sdf.format(calendar4.getTime()))){
                statistics.setDrillDown("周四新增");
                statistics.setData("周四");
                list.add(statistics);
            }if (statistics.getData().equals(sdf.format(calendar5.getTime()))){
                statistics.setDrillDown("周五新增");
                statistics.setData("周五");
                list.add(statistics);
            }
            if (statistics.getData().equals(sdf.format(calendar6.getTime()))){
                statistics.setDrillDown("周六新增");
                statistics.setData("周六");
                list.add(statistics);
            }
            if (statistics.getData().equals(sdf.format(calendar7.getTime()))){
                statistics.setDrillDown("周日新增");
                statistics.setData("周日");
                list.add(statistics);
            }
        }
        return list;
    }

    @Override
    public MonthList getMonthCountList() {

        List<Integer> list1 = new ArrayList<>();
        List<String> list2 = new ArrayList<>();
        MonthList monthList = new MonthList();

        List<MonthCount> monthCountList = statisticsMapper.getMonthCountList();
        for (MonthCount monthCount : monthCountList) {
            list1.add(monthCount.getCount());
            list2.add(monthCount.getMonthName());

        }
        monthList.setData(list1);
        monthList.setCategories(list2);

        return monthList;
    }
}














